package com.nbsaas.boot.evaluate.rest.resource;

import com.nbsaas.boot.evaluate.api.apis.EvaluateLikeApi;
import com.nbsaas.boot.entity.evaluate.EvaluateLike;
import com.nbsaas.boot.evaluate.api.domain.request.EvaluateLikeRequest;
import com.nbsaas.boot.evaluate.api.domain.response.EvaluateLikeResponse;
import com.nbsaas.boot.evaluate.api.domain.simple.EvaluateLikeSimple;
import com.nbsaas.boot.evaluate.rest.convert.EvaluateLikeSimpleConvert;
import com.nbsaas.boot.evaluate.rest.convert.EvaluateLikeEntityConvert;
import com.nbsaas.boot.evaluate.rest.convert.EvaluateLikeResponseConvert;
import com.nbsaas.boot.evaluate.data.repository.EvaluateLikeRepository;

import java.io.Serializable;
import com.nbsaas.boot.jpa.data.core.BaseResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.Resource;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;

import java.util.function.Function;
import com.nbsaas.boot.rest.enums.StoreState;
import com.nbsaas.boot.rest.request.RequestId;
import java.util.Optional;
import com.nbsaas.boot.rest.response.ResponseObject;
/**
*   评价点赞-业务接口实现
*/
@Transactional
@Service
public class EvaluateLikeResource extends BaseResource<EvaluateLike,EvaluateLikeResponse, EvaluateLikeSimple, EvaluateLikeRequest>  implements EvaluateLikeApi {

    @Resource
    private EvaluateLikeRepository evaluateLikeRepository;

    @Override
    public JpaRepositoryImplementation<EvaluateLike, Serializable> getJpaRepository() {
        return evaluateLikeRepository;
    }

    @Override
    public Function<EvaluateLike, EvaluateLikeSimple> getConvertSimple() {
        return new EvaluateLikeSimpleConvert();
    }

    @Override
    public Function<EvaluateLikeRequest, EvaluateLike> getConvertForm() {
        return new EvaluateLikeEntityConvert();
    }

    @Override
    public Function<EvaluateLike, EvaluateLikeResponse> getConvertResponse() {
        return new EvaluateLikeResponseConvert();
    }




    @Override
    public ResponseObject<EvaluateLikeResponse> create(EvaluateLikeRequest request) {
        request.setStoreState(StoreState.normal);
        return super.create(request);
   }

    @Override
    public ResponseObject<?> delete(RequestId request) {

        ResponseObject<?> result = new ResponseObject<>();
        Optional<EvaluateLike> optional = getJpaRepository().findById(request.getId());
        if (!optional.isPresent()) {
            result.setCode(501);
            result.setMsg("无效id");
            return result;
        }
        EvaluateLike bean = optional.get();
        bean.setStoreState(StoreState.draft);
        return result;

    }
}


